package service;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;


@Slf4j
@SpringBootTest(classes = cn.bugstack.Application.class)
public class ALipayTest {

    @Resource
    private AlipayClient alipayClient;

    // 查询指定订单的支付状态
    @Test
    public void queryOrderStatus() {
        String orderId = "4354823423213783"; // 替换为实际的订单ID
        try {
            // 创建查询请求
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
            bizModel.setOutTradeNo(orderId);
            request.setBizModel(bizModel);

            // 执行查询
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            String code = response.getCode();

            // 检查响应状态
            if ("10000".equals(code)) {
                log.info("订单 {} 支付状态查询成功，状态: {}", orderId, response.getTradeStatus());
            } else {
                log.error("订单 {} 支付状态查询失败，错误码: {}, 错误信息: {}", orderId, code, response.getMsg());
            }
        } catch (Exception e) {
            log.error("查询订单 {} 支付状态时发生异常", orderId, e);
        }
    }
}
